// pages/people-card/people-card.js 
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    people_level:'', //名片等级
    mine_level:'',   //自己等级
    is_website:false,//是否有官网
    is_friend:false, //是否交换名片
    hasCard: true,   //是否有名片
    // hasCard2:true,//是否有名交换
    checkCard:true,  //是否已认证名片
    navIdx:'',
    id:'',
    btmList:[
      { img1: '/img/card-logo1.png', img: '/img/card-logo.png', title: '名片' },
      { img1: '/img/card-home1.png', img: '/img/card-home.png', title: '官网' },
      { img1: '/img/news-logo1.png', img: '/img/news-logo.png', title: '聊天' },
    ]
  },

  backTap:function(){ //返回
    let pages = getCurrentPages();
    console.log(pages)
    if (pages.length==1){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }else{
      wx.navigateBack({
        delta: 1,
      })
    }
  },

  selectedNavTap: function (e) { //底部跳转
    if (!wx.getStorageSync('isLogin')) {
      wx.showModal({
        title: '提示',
        content: '您还未登录，确认登录吗？',
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
      return;
    }
    let that = this;
    var id = that.data.id;
    let idx = e.currentTarget.dataset.idx;
    console.log(idx);
    if (idx == that.data.navIdx) {
      return;
    }
    if (idx == 2 && that.data.user.is_card==0){ //未创建名片
      that.setData({
        hasCard:false,
      })
      return;
    }
    if (that.data.user.level_id<=0){
      wx.showToast({
        title: '抱歉，暂无权限进行此操作',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    that.setData({
      navIdx: idx
    })
    if (idx == 0) { //名片
      wx.redirectTo({
        url: '/pages/people-card/people-card?id=' +id,
      })
    } else if (idx == 1) { //官网
      wx.redirectTo({
        url: '/pages/people-home/people-home?id=' + id,
      })
    } else { //聊天
      wx.redirectTo({
        url: '/pages/chat/chat?id=' + id,
      })
    }
  },

  createCardTap:function(e){ //创建名片
    app.confirmLogin(
      function () {
        wx.navigateTo({
          url: '/pages/card-msg-add/card-msg-add',
        })
    })
  },

  checkCardTap:function(e){  //去认证名片
    app.confirmLogin(
      function () {
        wx.navigateTo({
          url: '/pages/card-check/card-check',
        })
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    let id = options.id;
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      console.log(scene)
      id = scene.split("=")[1];
      console.log(id)
      that.setData({
        id,
      })
    }else{
      that.setData({
        id,
      })
    }
    

    wx.showLoading({
      title: '加载中',
    })
    var user_id = wx.getStorageSync('userId')
    wx.request({
      url: app.globalData.host + 'member/getUserCardInfo',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        user_id: user_id,
        card_id: id,
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res)
        console.log(res.data.data.user)
        var _card = res.data.data.card.is_card;
        if(!_card){
          wx.showModal({
            content: '该用户未创建名片',
            showCancel:false,
            confirmText:'返回',
            success(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1,
                })
              } 
            }
          })
          return;
        }
        if (res.data.data.card.level){
          var people_level = Number(res.data.data.card.level.level)
        }else{
          var people_level = 0;
        }
        if (res.data.data.user.level) {
          var mine_level = Number(res.data.data.user.level.level)
        } else {
          var mine_level = 0;
        }
        console.log(people_level)
        that.setData({
          user: res.data.data.user,
          card: res.data.data.card,
          people_level: people_level,
          mine_level: mine_level,
          is_friend: res.data.data.is_friend,
          is_website: res.data.data.is_website,
        })
      }
    })
  },


  changeCard:function(e){ //交换名片
    if(!wx.getStorageSync('isLogin')){
      wx.showModal({
        title: '提示',
        content: '您还未登录，确认登录吗？',
        showCancel: true,
        success: function(res) {
          if(res.confirm){
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        },
        fail: function(res) {},
        complete: function(res) {},
      })
      return;
    }
    var that = this;
    var user = that.data.user
    if(user.is_card != 1){
      this.setData({
        hasCard: false,
      })
      return;
    }
    if (user.level_id<=0){
      wx.showToast({
        title: '抱歉，暂无权限进行此操作',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    // if (user.enabled == 0) { //认证
    //   this.setData({
    //     checkCard: false,
    //   })
    //   return;
    // }

    var formId = e.detail.formId;

    wx.showModal({
      title: '温馨提示',
      content: '是否交换名片？',
      showCancel: true,
      success: function(res) {
        if(res.confirm){

          app.getFormId(formId);
          var params = {
            user_id: wx.getStorageSync('userId'),
            card_id: that.data.id,
          };
          app.request(app.globalData.host + 'member/handleoverCard', params, 'POST', true,
            function (res) {
              console.log(res)
              if (res.data.code == 1) {
                wx.showToast({
                  title: '申请交换成功！',
                })
                return;
              }
              wx.showToast({
                title: res.data.msg,
                icon: '',
              })
            },
            function (err) { },
            function (complete) { })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  showModal:function(){
    var that = this;
    that.setData({
      hasCard: true,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})